package cn.iocoder.springboot.lab23.testdemo.service;

import cn.iocoder.springboot.lab23.testdemo.dao.UserDao;
import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import io.lettuce.core.RedisClient;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.client.RedisException;
import org.redisson.client.codec.StringCodec;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.IntStream;

@Slf4j
@Service
public class UserService {
    @Autowired
    private BloomFilterTool bloomFilterTool;

    @Autowired
    private UserDao userDao;

    public UserDO get(Integer id) {
        return userDao.selectById(id);
    }

    @Scheduled(fixedDelay = 10L)
    public void testBloomFilter() {
        doTestBloomFilter();
    }

    private void doTestBloomFilter() {
        Long start = System.currentTimeMillis();
        AtomicInteger ac = new AtomicInteger(0);
        IntStream.range(4500, 5500).parallel()
                .forEach(t -> {
                    boolean exsist = bloomFilterTool.containWordAndAdd(t + "");
                    if (exsist) ac.incrementAndGet();
                });
        Long end = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "存在:" + ac.get() + "耗时:" + (end - start) / 1000);
    }
}
